<template>
  <el-dialog
    :title="artData ? '修改文章' : '新增文章'"
    :visible.sync="addDiao"
    width="800px"
    :center="false"
    :before-close="cancel"
  >
    <el-form :model="form" :rules="rules" ref="myForm" label-width="100px">
      <el-form-item label="文章标题：" prop="title">
        <el-input v-model="form.title" placeholder="请输入文章标题"></el-input>
      </el-form-item>
      <el-form-item label="文章内容：" prop="articleBody">
        <!-- 富文本编辑器 -->
        <quill-editor ref="myQuillEditor" v-model="form.articleBody" :options="editorOption" />
        <el-input v-model="form.articleBody" style="display: none;"></el-input>
      </el-form-item>
      <el-form-item label="视频地址：" prop="videoURL">
        <el-input v-model="form.videoURL" placeholder="请输入视频地址"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// API 接口导入
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: ['addDiao', 'artData'],
  name: 'ArtAdd',
  data() {
    return {
      // 表单数据
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 验证规则
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }],
        videoURL: [
          {
            pattern: /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i,
            message: '请输入正确的URL视频地址',
            trigger: 'blur'
          }
        ]
      },
      // 富文本编辑器的设置
      editorOption: {
        placeholder: ''
      }
    }
  },
  methods: {
    // 取消面板显示
    cancel() {
      this.$emit('update:addDiao', false)
      // 重置面板的数据
      this.$refs.myForm.resetFields()
      // --------------------------------------------------------------------------------
      setTimeout(() => {
        this.$emit('update:artData', '')
      }, 1000)
    },
    // 传入的数据回填
    dataInit() {
      const { id, title, articleBody, videoURL } = this.artData
      this.form = { id, title, articleBody, videoURL }
      // console.log(this.form)
    },
    // 提交数据
    submit() {
      // 预校验
      this.$refs.myForm.validate(async valid => {
        // 没有通过验证则跳出
        if (!valid) return false
        // 判断是编辑还是添加
        if (this.artData) {
          try {
            // 修改文章请求
            await update(this.form)
            this.$message.success('修改文章成功')
            // 关闭面板
            this.cancel()
            this.$emit('updataList')
          } catch (error) {
            this.$message.error(error)
          }
        } else {
          try {
            // 添加文章
            await add(this.form)
            this.$message.success('添加文章成功')
            // 重新渲染外面列表的数据
            this.$emit('updataList')
            this.cancel()
          } catch (error) {
            this.$message.error(error)
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .footer {
  display: flex;
  justify-content: flex-end;
}
::v-deep .ql-container {
  height: 200px;
}
</style>
